﻿using CCWin;
using System;
using System.Windows.Forms;
using System.Data;

namespace DC.Views
{
    public partial class Form_Fee : Skin_DevExpress
    {


        public Form_Fee()
        {
            InitializeComponent();
        }

        Utils.Database DB = Utils.Global.GlobalDB;
        public string patientID = string.Empty;
        public string UserID = string.Empty;
        public int limit = -1;    // -1正常收费 -2是 作废  -3是退款
        public int Judge = 0;   //0为正常收费 1是修改收费
        public int Change_FeeID = 0;   //读取修改的FeeID
        public int debt = 0;        //病人历史欠款
        public int thisdebt = 0;     //这次欠款

        private void skinDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (skinDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
            {
                return;
            }
            switch (e.ColumnIndex)
            {
                case 0: // 编辑的是名称
                    Views.Form_ProjectFilter filter = new Form_ProjectFilter();
                    filter.searchText = skinDataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    filter.ShowDialog();
                    if (filter.ProjectID != string.Empty)
                    {
                        string name = DB.GetData("select Project_Name from Project where Project_ID=" + filter.ProjectID).ToString();
                        skinDataGridView1.Rows[e.RowIndex].Cells[0].Value = name;
                        string price = DB.GetData("select Project_Price from Project where Project_ID=" + filter.ProjectID).ToString();
                        skinDataGridView1.Rows[e.RowIndex].Cells[1].Value = price;
                        string suffix = DB.GetData("select Project_Suffix from Project where Project_ID=" + filter.ProjectID).ToString();
                        skinDataGridView1.Rows[e.RowIndex].Cells[2].Value = suffix;
                        skinDataGridView1.Rows[e.RowIndex].Cells[3].Value = 1;
                        skinDataGridView1.Rows[e.RowIndex].Cells[4].Value = price;
                        SumTotal();
                    }
                    break;
                case 1: //编辑的是单价
                    skinDataGridView1.Rows[e.RowIndex].Cells[4].Value = Convert.ToSingle(skinDataGridView1.Rows[e.RowIndex].Cells[1].Value) *
                                                                    Convert.ToInt32(skinDataGridView1.Rows[e.RowIndex].Cells[3].Value);
                    SumTotal();
                    break;
                case 3:
                    skinDataGridView1.Rows[e.RowIndex].Cells[4].Value = Convert.ToDouble(skinDataGridView1.Rows[e.RowIndex].Cells[1].Value) *
                                                Convert.ToInt32(skinDataGridView1.Rows[e.RowIndex].Cells[3].Value);
                    SumTotal();
                    break;
            }
        }

        private void SumTotal()
        {
            float sum = 0f;
            foreach (DataGridViewRow row in skinDataGridView1.Rows)
            {
                sum += Convert.ToSingle(row.Cells[4].Value);
                label7.Text = sum.ToString();
                label8.Text = (Convert.ToSingle(label6.Text) + Convert.ToSingle(label7.Text)).ToString();
            }
            label8.Text = (Convert.ToSingle(label6.Text) + Convert.ToSingle(label7.Text)).ToString();
        }

        private void Form_Fee_Load(object sender, EventArgs e)
        {
            //判断当前是修改收费还是正常收费
            if (Judge == 0)
            {
                this.Text = "新增收费";
            }
            else
            {
                this.Text = "修改收费";
                LoadFeeDetails();//读取上次收费项目列表
            }

            string sql = "select Patient_Debt from Patient where Patient_ID='" + patientID + "'";//读取上次欠费信息
            label6.Text = DB.GetData(sql).ToString();
            this.skinComboBox1.SelectedIndex = 1;
            SumTotal();
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要不保存直接退出吗？", "Ask", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (Judge == 0)
            {
                Normal_Fee();//正常收费
            }
            else
            {
                modify_Fee();//修改收费
            }
        }

        private void modify_Fee()
        {
            
        }

        private void LoadFeeDetails()
        {
            MessageBox.Show(Change_FeeID.ToString());
            string sql = @"select Charge_Project ,Charge_SinglePrice,Charge_Suffix, Charge_Num, Charge_TotalPrice 
                                FROM ChargeDetails WHERE Fee_ID =" + Change_FeeID;
            DataView FeeDetails = DB.GetDs(sql).Tables[0].DefaultView;
            
            for (int i = 0; i < FeeDetails.Count; i++)
            {
                skinDataGridView1.Rows.Add();
                skinDataGridView1.Rows[i].Cells[0].Value = FeeDetails[i][0].ToString();
                skinDataGridView1.Rows[i].Cells[1].Value = FeeDetails[i][1].ToString();
                skinDataGridView1.Rows[i].Cells[2].Value = FeeDetails[i][2].ToString();
                skinDataGridView1.Rows[i].Cells[3].Value = FeeDetails[i][3].ToString();
                skinDataGridView1.Rows[i].Cells[4].Value = FeeDetails[i][4].ToString();
            }

        }

        private void Normal_Fee()
        {
            if (skinTextBox2.Text == "0" || Convert.ToSingle(skinTextBox2.Text) > Convert.ToSingle(label8.Text))
            {
                MessageBox.Show("实收金额输入不合法");
                return;
            }
            if (MessageBox.Show("确实要结账吗，确认后不可更改？", "Ask", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string sql = string.Format(@"insert into Fee(Fee_Time,Patient_ID,User_ID,Fee_Price,Fee_Should,Fee_Real,Fee_Debt,Fee_Payment,Fee_Status)values(
                                                            '{0}',{1},{2},{3},{4},{5},{6},'{7}',{8})",
                                                            DateTime.Now.ToString(),
                                                            patientID,
                                                            UserID,
                                                            label7.Text,
                                                            label8.Text,
                                                            skinTextBox2.Text,
                                                            Convert.ToSingle(label8.Text) - Convert.ToSingle(skinTextBox2.Text),
                                                            skinComboBox1.Text,
                                                            limit   //1 正常收费
                                                            );
                DB.Execute(sql);

                string FeeID = DB.GetData("select top 1 Fee_ID from Fee where Patient_ID='" + patientID + "' order by Fee_ID DESC").ToString();
                for (int i = 0; i < skinDataGridView1.RowCount - 1; i++)
                {
                    DataGridViewRow row = skinDataGridView1.Rows[i];
                    string sql2 = string.Format(@"insert into ChargeDetails(Fee_ID,Charge_Project,Charge_SinglePrice,Charge_Suffix,Charge_Num,Charge_TotalPrice)values(
                                                                            {0},'{1}',{2},'{3}',{4},{5})",
                                                                            FeeID,
                                                                            row.Cells[0].Value.ToString(),
                                                                            row.Cells[1].Value.ToString(),
                                                                            row.Cells[2].Value.ToString(),
                                                                            row.Cells[3].Value.ToString(),
                                                                            row.Cells[4].Value.ToString()
                                                                            );
                    DB.Execute(sql2);
                }
                DB.Execute("update Patient set Patient_Debt=" + (Convert.ToSingle(label8.Text) - Convert.ToSingle(skinTextBox2.Text)).ToString()
                    + " where Patient_ID='" + patientID + "'");
                MessageBox.Show("结算成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            Form_AddRange add = new Form_AddRange();
            add.ShowDialog();
            if (!add.issuccess)
            {
                return;
            }

            foreach (int i in add.range)
            {
                string name = DB.GetData("select Project_Name from Project where Project_ID=" + i).ToString();
                string price = DB.GetData("select Project_Price from Project where Project_ID=" + i).ToString();
                string suffix = DB.GetData("select Project_Suffix from Project where Project_ID=" + i).ToString();
                int index = skinDataGridView1.Rows.Add();
                skinDataGridView1.Rows[index].Cells[0].Value = name;
                skinDataGridView1.Rows[index].Cells[1].Value = price;
                skinDataGridView1.Rows[index].Cells[2].Value = suffix;
                skinDataGridView1.Rows[index].Cells[3].Value = 1;
                skinDataGridView1.Rows[index].Cells[4].Value = price;
            }
            SumTotal();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (skinDataGridView1.SelectedCells.Count == 0)
            {
                return;
            }
            skinDataGridView1.Rows.Remove(skinDataGridView1.CurrentRow);
            SumTotal();
        }

        private void skinDataGridView1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void skinDataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo info = skinDataGridView1.HitTest(e.X, e.Y);
                skinDataGridView1[info.ColumnIndex, info.RowIndex].Selected = true;
            }
        }
    }
}
